package com.nitobi.test
{
	import com.nitobi.coop.model.CoopModel;
	import com.nitobi.coop.service.CoopService;
	import com.nitobi.util.Encryption;
	import com.nitobi.test.Constants;
	
	import flash.events.Event;
	
	import flexunit.framework.TestCase;
	
	public class CoopServiceTest extends TestCase
	{
		/**
		 * no test created for group daily entries
		 * since there may not be any entries for the group
		 * when the user runs the tests
		 * **/
		 
		private var _model:CoopModel;
		private var _service:CoopService;
		
		override public function setUp():void
		{
			Encryption.writeEncryptedStringSetting("coopUser", Constants.COOP_USER);
			Encryption.writeEncryptedStringSetting("coopPass", Constants.COOP_PASS);		
			
			_model = CoopModel.getInstance();
			_service = CoopService.getInstance();
			_service.init();
		}
		
		public function testCoopService():void
		{
			_service.getCoopGroupsForUser(addAsync(handleGetCoopGroupsForUser, Constants.TIMEOUT_MS), _service.coopError);
		}
		
		private function handleGetCoopGroupsForUser(evt:Event):void
		{
			// reset the model so we can test to see if populateGroupListTest will get the correct data
			_model.groupId = 0;
			_model.groupDailyEntriesAC.removeAll();
			_model.groupListAC.removeAll();
			// call the populate group list test
			populateGroupListTest(evt);
		}
		
		private function populateGroupListTest(evt:Event):void
		{
			_service.populateGroupList(evt);
			
			assertTrue("groupId retreived", _model.groupId > 0);
			assertTrue("more than 1 group returned for the user", _model.groupListAC.length > 0);
		}

	}
}